console.log('🧪 直接调试 wsHandlers 逻辑...');

// 模拟 wsHandlers 对象
const wsHandlers = {
    3: async (payload, helpers) => {
        console.log('🔍 [DEBUG] 处理code 3请求，userId:', helpers.userId);
        console.log('🔍 [DEBUG] payload:', payload);

        // 模拟返回数据
        const response = {
            act_id: 3,
            roleCount: 1,
            roleList: [
                {
                    allowLogin: 1,
                    headImg: "head_1",
                    id: 2001,
                    lvl: 1,
                    name: "测试角色",
                    roleType: 1,
                    sex: 1,
                }
            ],
            sid: "test_sid_123",
        };

        console.log('🔍 [DEBUG] 返回响应:', response);
        return response;
    }
};

// 模拟消息处理逻辑
function simulateMessageHandling(c, d) {
    console.log(`🔍 [DEBUG] 模拟处理消息: c=${c}, d=${JSON.stringify(d)}`);
    console.log(`🔍 [DEBUG] c 的类型:`, typeof c);
    console.log(`🔍 [DEBUG] Number.isFinite(Number(c)):`, Number.isFinite(Number(c)));
    console.log(`🔍 [DEBUG] wsHandlers[${c}] 存在:`, !!wsHandlers[Number(c)]);

    if (Number.isFinite(Number(c)) && wsHandlers[Number(c)]) {
        console.log(`🔍 [DEBUG] 准备调用 wsHandlers[${c}]`);

        // 模拟 helpers 对象
        const mockHelpers = {
            userId: 4,
            selectedCharacterId: undefined,
            characterService: {},
            redisService: {},
            chatMessageService: {},
            announcementService: {},
            itemService: {},
            inventoryService: {},
            monthlyCardService: {},
            characterSkillService: {},
            shopService: {},
        };

        // 调用处理器
        wsHandlers[Number(c)](d, mockHelpers).then(result => {
            console.log(`✅ wsHandlers[${c}] 调用成功，返回:`, result);
        }).catch(error => {
            console.error(`❌ wsHandlers[${c}] 调用失败:`, error);
        });
    } else {
        console.log(`❌ code ${c} 不在 wsHandlers 中或不是数字`);
    }
}

// 测试不同的消息格式
console.log('\n=== 测试 1: 数字 code ===');
simulateMessageHandling(3, null);

console.log('\n=== 测试 2: 字符串 code ===');
simulateMessageHandling("3", null);

console.log('\n=== 测试 3: 无效 code ===');
simulateMessageHandling("invalid", null); 